// file: $isip/class/asr/AnnotationIndex/AnnotationIndex.h
// version: $Id: AnnotationIndex.h 8665 2002-09-01 18:42:02Z alphonso $
//

// make sure definitions are only made once
//
#ifndef ISIP_ANNOTATION_INDEX
#define ISIP_ANNOTATION_INDEX

#ifndef ISIP_STRING
#include <String.h>
#endif

#ifndef ISIP_HASH_TABLE
#include <HashTable.h>
#endif

#ifndef ISIP_HASH_KEY
#include <HashKey.h>
#endif

#ifndef ISIP_DOUBLE_LINKED_LIST
#include <DoubleLinkedList.h>
#endif

#ifndef ISIP_ANNOTATION
#include <Annotation.h>
#endif

#ifndef ISIP_ANCHOR
#include <Anchor.h>
#endif

// AnnotationIndex: a class used for quick access to annotations and anchors
//
// Reference:
//
// [1] S. Bird, M. Liberman, A Formal Framework for Linguistic Annotation,
// Linguistic Data Consortium, University of Pennsylvania, Philadelphia,
// Pennsylvania, USA, 2000.
//
// [2] K. Maeda, X. Ma, H. Lee, S. Bird, The Annotation Graph Toolkit:
// Application Developer's Manual (Draft), Linguistic Data Consortium,
// University of Pennsylvania, Philadelphia, Pennsylvania, USA, 2001.
//
class AnnotationIndex {
  
  //--------------------------------------------------------------------------
  //
  // public constants
  //
  //--------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  // i/o related constants
  //
  static const String DEF_PARAM;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
  protected:

  // define a list to store the set of anchors
  //
  DoubleLinkedList<Anchor> ancrset_d;

  // define the anchors indexed by identifiers
  //
  HashTable<String, Anchor> ancr_by_id_d;

  // define the anchor set indexed by the offset
  //  
  HashTable<Float, DoubleLinkedList<Anchor> > ancrset_by_offset_d;

  // define the annotations indexed by identifiers
  //
  HashTable<String, Annotation> anno_by_id_d;

  // define the annotations indexed by type
  //
  HashTable<String, DoubleLinkedList<Annotation> > anno_by_type_d;
  
  // define annotations indexed by the offset of start anchor
  //
  HashTable<Float, DoubleLinkedList<Annotation> > start_by_offset_d;
  
  // define annotations indexed by the offset of end anchor
  //
  HashTable<Float, DoubleLinkedList<Annotation> > end_by_offset_d;
  
  // define annotations indexed by their end anchor
  //
  HashTable<HashKey<Anchor>, DoubleLinkedList<Annotation> > incoming_d;
  
  // define annotations indexed by their start anchor
  //
  HashTable<HashKey<Anchor>, DoubleLinkedList<Annotation> > outgoing_d;

  // define annotations indexed by their feature
  //
  HashTable<String, DoubleLinkedList<Annotation> > by_feature_d;    
  
  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG debug_level_d;
  
  // a static memory manager
  //
  static MemoryManager mgr_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
  
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }
  
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods
  //
  boolean debug(const unichar* msg) const;

  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG arg) {
    debug_level_d = arg;
    return true;
  }
  
  // method: destructor
  //
  ~AnnotationIndex();

  // method: default constructor
  //
  AnnotationIndex();

  // method: copy constructor
  //
  AnnotationIndex(const AnnotationIndex& arg) {
    assign(arg);
  }

  // method: assign
  //
  boolean assign(const AnnotationIndex& arg);
  
  // method: sofSize
  //
  long sofSize() const {
    return 0;
  }

  // method: read
  //
  boolean read(Sof& sof, long tag) {
    return read(sof, tag, name());
  }

  // method: read
  //  
  boolean read(Sof& sof, long tag, const String& name) {
    return true;
  }
  
  // method: readData
  //  
  boolean readData(Sof& sof, const String& pname = DEF_PARAM, long size =
		   SofParser::FULL_OBJECT, boolean param = true,
		   boolean nested = false) {
    return true;
  }
  
  // method: write
  //
  boolean write(Sof& sof, long tag) const {
    return write(sof, tag, name());
  }

  // method: write
  //  
  boolean write(Sof& sof, long tag, const String& name) const {
    return true;
  }

  // method: writeData
  //    
  boolean writeData(Sof& sof, const String& pname =  DEF_PARAM) const {
    return true;
  }
  
  // method: eq
  //
  boolean eq(const AnnotationIndex& arg) const;

  // method: new
  //
  static void* operator new(size_t arg) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t arg) {
    return mgr_d.getBlock(arg);
  }

  // method: delete
  //
  static void operator delete(void* arg) {
    mgr_d.release(arg);
  }

  // method: delete[]
  //
  static void operator delete[](void* arg) {
    mgr_d.releaseBlock(arg);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long arg) {
    return mgr_d.setGrow(arg);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE cmode = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //
  //---------------------------------------------------------------------------

  // method to add an annotation to the indexes
  //
  boolean add(Annotation* a);
  
  // method to delete an annotation from the indexes
  //
  boolean deleteAnnotation(Annotation* anno);

  // method to check if the specified annotation exists
  //
  boolean existsAnnotation(const String& id);
  
  // method to check if the specified anchor exists
  //
  boolean existsAnchor(const String& id);
  
  // method to get the nearest used offset to the specified offset
  //
  float getNearestOffset(float offset);
  
  // method that gets the annotations that overlap a particular time offset.
  // gets all annotations whose start anchor offset is smaller than or
  // equal to the given offset AND end anchor offset is greater than 
  // or equal to the given offet
  //
  boolean getByOffset(float offset, DoubleLinkedList<Annotation>& list);
  
  // method get one of the annotations which overlap a particular time offset.
  // same as getByOffset except that getAnnotationByOffset returns only
  // one qualified annotation while getByOffset returns all of them
  //
  Annotation* getAnnotationByOffset(float offset);

  // method to add a feature to the indexes
  //
  boolean addFeature(Annotation* anno, const String& feature,
		     const String& value);
  
  // method to delete a feature from the indexes
  //
  boolean deleteFeature(Annotation* anno, const String& feature);

  // method to check if the feature-value pait exists
  //
  boolean existsFeature(const String& feature, const String& value);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
  private:

  // friend class
  //
  friend class AnnotationGraph;
  
  // method to add an anchor to the indexes
  //
  boolean addAnchor(Anchor* ancr);
  
  // method to delete an anchor from the indexes
  //
  boolean deleteAnchor(Anchor*ancr);
};

//end of include file
//
#endif
