GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-35ebcb33a3
mem_stream.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * MODULE: iostream
4  *
5 
6  * COPYRIGHT (C) 2007 Laura Toma
7  *
8  *
9 
10  * Iostream is a library that implements streams, external memory
11  * sorting on streams, and an external memory priority queue on
12  * streams. These are the fundamental components used in external
13  * memory algorithms.
14 
15  * Credits: The library was developed by Laura Toma. The kernel of
16  * class STREAM is based on the similar class existent in the GPL TPIE
17  * project developed at Duke University. The sorting and priority
18  * queue have been developed by Laura Toma based on communications
19  * with Rajiv Wickremesinghe. The library was developed as part of
20  * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
21  * Rajiv Wickremesinghe as part of the Terracost project.
22 
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29 
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * General Public License for more details. *
34  * **************************************************************************/
35 
36 #ifndef _MEM_STREAM_H
37 #define _MEM_STREAM_H
38 
39 #include <stdlib.h>
40 #include <assert.h>
41 
42 #include <cstring>
43 #include <iostream>
44 
45 template <class T>
46 class MEM_STREAM {
47 private:
48  T *data;
49  T *curr;
50  T *dataend;
51  int len;
52 
53 public:
54  MEM_STREAM(T *data, int len);
55  ~MEM_STREAM(void);
56 
57  // Read and write elements.
58  AMI_err read_item(T **elt);
59 
60  AMI_err write_item(const T &elt);
61 
62  // Return the number of items in the stream.
63  off_t stream_len(void);
64 
65  // Return the path name of this stream.
66  AMI_err name(char **stream_name);
67 
68  // Move to a specific item in the stream.
69  AMI_err seek(off_t offset);
70 
71  char *sprint();
72 };
73 
74 /**********************************************************************/
75 
76 template <class T>
77 MEM_STREAM<T>::MEM_STREAM(T *datap, int lenv)
78 {
79 
80  data = datap;
81  dataend = data + lenv;
82  curr = datap;
83  len = lenv;
84 }
85 
86 /**********************************************************************/
87 // Return the number of items in the stream.
88 template <class T>
90 {
91 
92  return len;
93 }
94 
95 /**********************************************************************/
96 // Return the path name of this stream.
97 template <class T>
98 AMI_err MEM_STREAM<T>::name(char **stream_name)
99 {
100 
101  const char *path = "dummy";
102 
103  *stream_name = new char[strlen(path) + 1];
104  strcpy(*stream_name, path);
105 
106  return AMI_ERROR_NO_ERROR;
107 }
108 
109 /**********************************************************************/
110 // Move to a specific offset within the (sub)stream.
111 template <class T>
113 {
114 
115  assert(offset <= len);
116 
117  curr = data + offset;
118 
119  return AMI_ERROR_NO_ERROR;
120 }
121 
122 /**********************************************************************/
123 template <class T>
125 {
126 }
127 
128 /**********************************************************************/
129 template <class T>
131 {
132 
133  assert(data);
134 
135  if (curr == dataend) {
137  }
138  *elt = curr;
139  curr++;
140  return AMI_ERROR_NO_ERROR;
141 }
142 
143 /**********************************************************************/
144 
145 template <class T>
147 {
148 
149  assert(data);
150 
151  if (curr == dataend) {
153  }
154  *curr = elt;
155  curr++;
156  return AMI_ERROR_NO_ERROR;
157 }
158 
159 /**********************************************************************/
160 // sprint()
161 // Return a string describing the stream
162 //
163 // This function gives easy access to the file name, length.
164 // It is not reentrant, but this should not be too much of a problem
165 // if you are careful.
166 template <class T>
168 {
169  static char buf[BUFSIZ];
170  snprintf(buf, sizeof(buf), "[MEM_STREAM %d]", stream_len());
171  return buf;
172 }
173 
174 #endif // _MEM_STREAM_H
AMI_err
Definition: ami_stream.h:83
@ AMI_ERROR_END_OF_STREAM
Definition: ami_stream.h:86
@ AMI_ERROR_NO_ERROR
Definition: ami_stream.h:84
AMI_err seek(off_t offset)
Definition: mem_stream.h:112
AMI_err write_item(const T &elt)
Definition: mem_stream.h:146
off_t stream_len(void)
Definition: mem_stream.h:89
MEM_STREAM(T *data, int len)
Definition: mem_stream.h:77
~MEM_STREAM(void)
Definition: mem_stream.h:124
char * sprint()
Definition: mem_stream.h:167
AMI_err read_item(T **elt)
Definition: mem_stream.h:130
AMI_err name(char **stream_name)
Definition: mem_stream.h:98
#define assert(condition)
Definition: lz4.c:393
#define strcpy
Definition: parson.c:62
Definition: path.h:15